#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>

#include "sqlite_manager.h"
#include "common.h"
#include "taskinfo.h"
#include "list.h"

sqlite3 *DB;

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i;
    for (i = 0; i < argc; i++)
    {
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

int OpenDataBase(void)
{
    int rc = 0;
    /* Open database */
    rc = sqlite3_open(DATABASE_FILE_NAME, &DB);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(DB));
        return RETURN_FAILED;
    }
    else
    {
        fprintf(stdout, "Opened database successfully\n");
    }

    return RETURN_OK;
}

int CloseDataBase(void)
{
    if (DB)
    {
        sqlite3_close(DB);
        DB = NULL;
    }

    return RETURN_OK;
}

int ExecuteSql(char *sql)
{
    char *zErrMsg = 0;
    int rc = 0;

    /* Execute SQL statement */
    rc = sqlite3_exec(DB, sql, callback, 0, &zErrMsg);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return RETURN_FAILED;
    }

    return RETURN_OK;
}

int InitTable(void)
{
    /* Create SQL statement */
    char *sql = "CREATE TABLE IF NOT EXISTS gtktable("
                "ID integer PRIMARY KEY  autoincrement   NOT NULL,"
                "filename varchar(256),    "
                "url varchar(1024),    "
                "size   INT,"
                "progress  REAL default 0.0,"
                "status  int  ,"
                "create_time  TIMESTAMP  DEFAULT CURRENT_TIMESTAMP);";

    return ExecuteSql(sql);
}

int GetLastInsertRowId(void)
{
    return sqlite3_last_insert_rowid(DB);
}

int GetTableInfo(void)
{
    int rc = 0;
    char **dbresult;
    int nRow, nColum;
    char *zErrMsg;
    int index = 0, i = 0, j = 0;
    Taskinfo t = {0};

    rc = sqlite3_get_table(DB, "SELECT  * FROM gtktable", &dbresult, &nRow, &nColum, &zErrMsg);
    if (rc == SQLITE_OK)
    {
        index = nColum;
        //printf("row:%d, col:%d\n", nRow, nColum);
        for (i = 0; i < nRow; i++)
        {
            t.id = atoi(dbresult[index++]);
            strcpy(t.filename, dbresult[index++]);
            strcpy(t.url, dbresult[index++]);
            t.size = atoi(dbresult[index++]);
            t.progress = atoi(dbresult[index++]);
            char *status = dbresult[index++];
            t.status = status ? atoi(status) : 0;
            strcpy(t.create_time, dbresult[index++]);
            //准备好Taskinfo添加链表
            AddItem(&t);
            //  ++index;
            //PrintTaskInfo(&t);

            //printf("----------------------------------------------\n");
        }
    }
    sqlite3_free_table(dbresult);
}

int GetTableInfoSingle(int id, Taskinfo *t)
{
    int rc = 0;
    char **dbresult;
    int nRow, nColum;
    char *zErrMsg;
    int index = 0, i = 0, j = 0;
    char cmd[SQL_CMD_LEN] = {0};
    sprintf(cmd, "SELECT  * FROM gtktable WHERE id = %d", id);

    rc = sqlite3_get_table(DB, cmd, &dbresult, &nRow, &nColum, &zErrMsg);
    if (rc == SQLITE_OK)
    {
        index = nColum;
        //printf("row:%d, col:%d\n", nRow, nColum);
        for (i = 0; i < nRow; i++)
        {
            t->id = atoi(dbresult[index++]);
            strcpy(t->filename, dbresult[index++]);
            strcpy(t->url, dbresult[index++]);
            t->size = atoi(dbresult[index++]);
            t->progress = atoi(dbresult[index++]);
            char *status = dbresult[index++];
            t->status = status ? atoi(status) : 0;
            strcpy(t->create_time, dbresult[index++]);
            //准备好Taskinfo添加链表
            //  ++index;
            //

            //printf("----------------------------------------------\n");
        }
    }
    sqlite3_free_table(dbresult);
}

/*
int InsertTaskInfo(void)
{
    
    char *sql = "CREATE TABLE IF NOT EXISTS gtktable("
          "ID integer PRIMARY KEY  autoincrement   NOT NULL,"
          "filename varchar(256),    "
          "url varchar(1024),    "
          "size   INT,"
          "progress  REAL default 0,"
          "status  int  ,"
          "create_time  TIMESTAMP  DEFAULT CURRENT_TIMESTAMP);";

    return ExecuteSql(sql);
}
*/